
import { JXDef } from "../../../conventions/JXCommon";
import { PRIORITY_VIEW } from "../../../Core/CoreDefine";
import { GCtrl } from "../../../Core/GCtrl";
import GChild from "../../../Core/GView/GChild";
import ShaderUtil from "../../../Shaders/Manager/ShaderUtil";
import { BOXPREVIEW, CMsg, PointInfo, RedpointEffect, RPointMask, VIPBOXTYPE, VIPRECEIVE } from "../../Common/Define";
import { VIEW_ID } from "../../Common/UI";
import UICreate from "../../Common/UICreate";
import GameMgr from "../../Logic/GameMgr";

const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Home/VipPage')

export default class VipPage extends GChild {
    @property({
        type: cc.Node,
        tooltip: "vipbuff信息"
    })
    vipBuffLabel: cc.Node = null;

    @property({
        type: cc.Label,
        tooltip: "vipbuff"
    })
    vipBuffLab: cc.Label = null;

    @property({
        type: cc.Label,
        tooltip: "vipLv"
    })
    vipLv: cc.Label = null;

    @property({
        type: cc.Node,
        tooltip: "vipbuff信息"
    })
    rewardBtn: cc.Node[] = [];

    @property({
        type: cc.Color,
        tooltip: "颜色"
    })
    colors: cc.Color[] = [];

    @property({
        type: cc.Node,
        tooltip: "预览宝箱"
    })
    preview: cc.Node[] = [];

    @property({
        type: cc.Node,
        tooltip: "预览生成位置"
    })
    previewPos: cc.Node[] = [];

    @property({
        type: cc.Label,
        tooltip: "按钮文案"
    })
    boxLab: cc.Label[] = [];



    private _staticData: SVipDataRaw = null;
    private _index: number = null;


    public onGLoad(): void {
        GCtrl.ES.on(CMsg.client.home.onVipLvUp, this, this.setRedPoint.bind(this), PRIORITY_VIEW);
        this.initEvent();
    }

    /**初始触摸事件 */
    protected initEvent() {
        this.preview[VIPBOXTYPE.DAYBOX].on(cc.Node.EventType.TOUCH_START, this._onTouchStar0, this);
        this.preview[VIPBOXTYPE.FIRSTBOX].on(cc.Node.EventType.TOUCH_START, this._onTouchStar1, this);
    }

    _onTouchStar1(event: cc.Event.EventTouch) {
        this.preViewShow(BOXPREVIEW.FIRSTBOX);
    }

    _onTouchStar0(event: cc.Event.EventTouch) {
        this.preViewShow(BOXPREVIEW.DAYBOX);
    }


    protected preViewShow(index: number) {
        GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.previewCtrl, this._staticData, index);
    }


    public init(data: SVipDataRaw, index: number) {
        this._staticData = data;
        this._index = Number(index);
        let str: string = '' + index + '';
        // this.vipLv.string = str;
        let vipStr = UICreate.createDescribe(str);
        vipStr.setPosition(0, 0);
        this.vipLv.node.addChild(vipStr);


        // this.vipLv.node.addComponent(cc.RenderComponent);
        this.initBuffInfo();
        let vipData = GameMgr.vipData.data.values();
        let vipReceiverArr = GameMgr.lUserData.vipReward;
        let dayReceiveDate = GameMgr.lUserData.dayReceiveDate;
        if (dayReceiveDate.length < vipData.length) {
            dayReceiveDate.push(VIPRECEIVE.No);
            GameMgr.lUserData.dayReceiveDate = dayReceiveDate;
        }

        if (vipReceiverArr.length < vipData.length) {
            // console.log("1111111");

            let obj: vipReward = {
                recycle: VIPRECEIVE.No,
                recycleUp: VIPRECEIVE.No,
            }
            vipReceiverArr.push(obj);
            GameMgr.lUserData.vipReward = vipReceiverArr;
        }

        // console.log(" GameMgr.lUserData.vipReward", GameMgr.lUserData.vipReward);

        this.setRedPoint();
    }



    protected setRedPoint() {
        // console.log("this._index", this._index);

        let isDaily = GameMgr.userMgr.checkVipReward(this._staticData.id, false);
        let isFirst = GameMgr.userMgr.checkVipReward(this._staticData.id, true);


        let dayBool = GameMgr.userMgr.vipReward(this._staticData.id, false);
        let firstBool = GameMgr.userMgr.vipReward(this._staticData.id, true);

        dayBool ? this.boxLab[1].string = '已领取' : this.boxLab[1].string = '领取';
        firstBool ? this.boxLab[0].string = '已领取' : this.boxLab[0].string = '领取';

        isDaily ? ShaderUtil.normal(this.rewardBtn[1], true) : ShaderUtil.gray(this.rewardBtn[1], true);
        isFirst ? ShaderUtil.normal(this.rewardBtn[0], true) : ShaderUtil.gray(this.rewardBtn[0], true);
        GameMgr.redMgr.setRPointView(this.rewardBtn[1], this.rewardBtn[1], isDaily, RedpointEffect.REDPOINT, null);
        GameMgr.redMgr.setRPointView(this.rewardBtn[0], this.rewardBtn[0], isFirst, RedpointEffect.REDPOINT, null);
    }

    /**初始化buff*/
    protected initBuffInfo() {
        let str = this._staticData.txt;
        this.vipBuffLab.string = str;
        // this.assetImpl.font(Res.font.hkyt, (font) => {
        //     let lab = UICreate.label(str);
        //     lab.fontSize = 100;
        //     lab.lineHeight = 100;
        //     lab.fontFamily = font;
        //     this.vipBuffLabel.addChild(lab.node)
        // })
    }

    protected initGiftReward() {
        let type = GameMgr.lUserData.getVipReward(this._index - 1, VIPBOXTYPE.DAYBOX);
        if (GameMgr.lUserData.vipLv >= this._index) {
            if (type == VIPRECEIVE.FIRSTNORECEIVE) {
                this.boxLab[0].string = '已领取';
                GameMgr.lUserData.setVipReward(this._index - 1, VIPBOXTYPE.DAYBOX, VIPRECEIVE.FIRSTRECEIVE);
                GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, this._staticData.recycle);
                this.setRedPoint();
                GameMgr.redMgr.sendValueSettingMsg(RPointMask.RPM_Vip_Frist, false)
                GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.VipCtrl, { msg: PointInfo.vip_frist })
            }
        }
    }

    protected initDayGiftReward() {
        let firstType = GameMgr.lUserData.getVipReward(this._index - 1, VIPBOXTYPE.FIRSTBOX);
        if (GameMgr.lUserData.vipLv >= this._index) {
            if (firstType == VIPRECEIVE.DATNORECEIVE) {
                this.boxLab[1].string = '已领取';
                GameMgr.lUserData.setVipReward(this._index - 1, VIPBOXTYPE.FIRSTBOX, VIPRECEIVE.DAYRECEIVE);
                GameMgr.lUserData.useDayReceiveDate((this._index - 1));
                GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, this._staticData.recycleUp);
                this.setRedPoint();
                GameMgr.redMgr.sendValueSettingMsg(RPointMask.RPM_Vip_Daily, false)
                GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.VipCtrl, { msg: PointInfo.vip_daily })
            }

        }
    }
}  